package risk.control.project.service;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisPool;
import risk.control.project.entity.repository.UserBean;
import risk.control.project.framework.PostService;
import risk.control.project.mapper.user.UserMapper;

import java.util.HashMap;


@Service
public class UserService extends PostService {

    @Qualifier("MainSqlSessionFactory")
    private final SqlSessionFactory sqlSessionFactory;

    @Qualifier("JedisPool")
    private final JedisPool jedisPool;


    public UserService( JedisPool jedisPool, SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
        this.jedisPool = jedisPool;
    }


    public UserBean getUserForName(String userName) {
        try (SqlSession session = sqlSessionFactory.openSession(true)) {
            UserMapper mapper = session.getMapper(UserMapper.class);
            HashMap<String, String> params = new HashMap<>();
            params.put("userName", userName);
            return mapper.getUserForName(params);
        }
    }

    public UserBean getUserForId(String userId) {
        try (SqlSession session = sqlSessionFactory.openSession(true)) {
            UserMapper mapper = session.getMapper(UserMapper.class);
            HashMap<String, String> params = new HashMap<>();
            params.put("userId", userId);
            return mapper.getUserForId(params);
        }
    }


}
